<template>
  <list-card>
    <template v-slot:search>
      <el-form ref="queryForm" inline :model="queryParams" size="small">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="queryParams.name" clearable placeholder="请输入角色名称"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="角色编码" prop="code">
          <el-input v-model="queryParams.code" clearable placeholder="请输入角色编码"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" clearable placeholder="角色状态">
            <el-option v-for="item in localDict.role_status" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template v-slot:operate>
      <el-button v-has-permi="['system:role:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <el-button v-has-permi="['system:role:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
<!--      <el-button v-has-permi="['system:role:remove']" icon="el-icon-data-board" plain size="mini"-->
<!--                 @click="handleRecycle">回收站-->
<!--      </el-button>-->
<!--      <el-button v-has-permi="['system:role:export']" icon="el-icon-download" plain size="mini" type="warning"-->
<!--                 @click="handleExport">导出-->
<!--      </el-button>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </template>
    <template v-slot:content>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column label="角色编号" prop="code" width="120"/>
        <el-table-column :show-overflow-tooltip="true" label="角色名称" prop="name" width="150"/>
        <el-table-column label="角色类型" prop="type" width="150">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.role_type" :value="scope.row.type" show-label/>
          </template>
        </el-table-column>
        <el-table-column label="权限范围" prop="scope" width="150">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.role_scope" :value="scope.row.scope" show-label/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" width="100">
          <template v-slot:default="scope">
            <dict-tag :options="localDict.role_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="显示顺序" prop="sort" width="100"/>
        <el-table-column align="center" label="创建时间" prop="createTime" width="180"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template v-slot:default="scope">
            <el-button v-has-permi="['system:role:detail']" icon="el-icon-tickets" size="mini" type="text"
                       @click="handleDetail(scope.row)">详情
            </el-button>
            <el-button v-has-permi="['system:role:remove']" icon="el-icon-delete" size="mini" type="text"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total>0" :limit.sync="pageParam.pageSize" :page.sync="pageParam.pageNum"
                  :total="pageParam.total" @pagination="getList"/>
    </template>
    <model-card ref="model" @ok="getList"/>
    <recycle-card ref="recycle" :base-url="baseUrl" :columns="columns" :dicts="localDict" @ok="getList"/>
  </list-card>
</template>

<script>
import {getStaticData} from "@/api/common";
import {list, remove} from "@/api/base";
import ModelCard from "./components/ModelCard.vue";

export default {
  name: "Role",
  components: {ModelCard,},
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      baseUrl: '/system/role',
      // 显示搜索条件
      showSearch: false,
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        name: undefined,
        code: undefined,
        status: undefined
      },
      columns: [
        {label: '角色编号', props: 'code', dict: false, visible: true, width: 100},
        {label: '角色名称', props: 'name', dict: false, visible: true, width: 100},
        {label: '角色类型', props: 'type', dict: true, code: 'role_type', tag: true, visible: true, width: 140},
        {label: '数据权限', props: 'scope', dict: true, code: 'role_scope', tag: true, visible: true, width: 140},
      ],
      localDict: {
        role_type: [],
        role_scope: [],
        role_status: [],
      },

    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery();
  },
  methods: {
    async getLocalDict() {
      await this.getStaticData('system-role-type').then(res => {
        this.localDict.role_type = res.data
      })
      await this.getStaticData('system-role-scope').then(res => {
        this.localDict.role_scope = res.data
      })
      await this.getStaticData('system-status').then(res => {
        this.localDict.role_status = res.data
      })
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
          this.dataList = response.rows;
          this.pageParam.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.model.add();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.model.edit(row.id)
    },
    handleDetail(row) {
      console.log("selected row:=====", row)
      this.$router.push({name: 'RoleDetail', path: '/system/role/detail/' + row.id, params: {id: row.id}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的角色？').then(() => {
        remove(this.baseUrl, ids, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        })
      });
    },
    handleRecycle() {
      this.$refs.recycle.visible = true
      this.$refs.recycle.title = "角色管理回收站"
      this.$refs.recycle.resetQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

